{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "2af695ba-02c5-435f-b62c-e8f04f05fa24",
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import numpy as np\n",
    "from sklearn.feature_extraction import DictVectorizer\n",
    "from sklearn.model_selection import train_test_split\n",
    "from sklearn.tree import DecisionTreeClassifier, export_graphviz\n",
    "import matplotlib.pyplot as plt\n",
    "import seaborn as sns\n",
    "import warnings\n",
    "warnings.filterwarnings('ignore')\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "4ed462a3-06ec-4121-ad56-e9f44877790a",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 1、获取数据\n",
    "train = pd.read_csv(\"train.csv\")\n",
    "test = pd.read_csv(\"test.csv\")\n",
    "datas = pd.concat([train,test])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "90289df1-3020-46ca-8c8c-83908ae88293",
   "metadata": {},
   "outputs": [],
   "source": [
    "datas[datas['Fare'].isnull()]\n",
    "datas['Fare']=datas['Fare'].fillna(7.8)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "85ce32f6-c9d8-4be7-a400-e614291dfe48",
   "metadata": {},
   "outputs": [],
   "source": [
    "datas['Embarked'] = datas['Embarked'].fillna('C')\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "116e4bc5-0372-495e-bd96-97234d1de3af",
   "metadata": {},
   "outputs": [],
   "source": [
    "datas['Cabin']=datas['Cabin'].fillna(\"U\")\n",
    "datas['Cabin']=datas['Cabin'].str.get(0)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "64489a7e-f7fc-4c8d-b1f5-49258d923481",
   "metadata": {},
   "outputs": [],
   "source": [
    "from sklearn.ensemble import RandomForestRegressor\n",
    "ages = datas[['Age', 'Pclass','Sex']]\n",
    "ages=pd.get_dummies(ages)\n",
    "known_ages = ages[ages.Age.notnull()].values\n",
    "unknown_ages = ages[ages.Age.isnull()].values\n",
    "y = known_ages[:, 0]\n",
    "X = known_ages[:, 1:]\n",
    "rfr = RandomForestRegressor(random_state=60, n_estimators=100, n_jobs=-1)\n",
    "rfr.fit(X, y)\n",
    "pre_ages = rfr.predict(unknown_ages[:, 1::])\n",
    "datas.loc[ (datas.Age.isnull()), 'Age' ] = pre_ages\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "906c8a47-eafe-47d0-af77-6d271268251a",
   "metadata": {},
   "outputs": [],
   "source": [
    "datas['Title'] = datas['Name'].apply(lambda x:x.split(',')[1].split('.')[0].strip())\n",
    "datas['Title'].replace(['Capt', 'Col', 'Major', 'Dr', 'Rev'],'Officer', inplace=True)\n",
    "datas['Title'].replace(['Don', 'Sir', 'the Countess', 'Dona', 'Lady'], 'Royalty', inplace=True)\n",
    "datas['Title'].replace(['Mme', 'Ms', 'Mrs'],'Mrs', inplace=True)\n",
    "datas['Title'].replace(['Mlle', 'Miss'], 'Miss', inplace=True)\n",
    "datas['Title'].replace(['Master','Jonkheer'],'Master', inplace=True)\n",
    "datas['Title'].replace(['Mr'], 'Mr', inplace=True)\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "13fb46e8-488a-42bc-83b2-e40b33f2b2de",
   "metadata": {},
   "outputs": [],
   "source": [
    "datas['Fam_size'] = datas['SibSp'] + datas['Parch'] + 1\n",
    "\n",
    "datas.loc[datas['Fam_size']>7,'Fam_type']=0\n",
    "datas.loc[(datas['Fam_size']>=2)&(datas['Fam_size']<=4),'Fam_type']=2\n",
    "datas.loc[(datas['Fam_size']>4)&(datas['Fam_size']<=7)|(datas['Fam_size']==1),'Fam_type']=1\n",
    "datas['Fam_type']=datas['Fam_type'].astype(np.int32)\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "7d0d7cae-597d-45db-a401-59170bf3c211",
   "metadata": {},
   "outputs": [],
   "source": [
    "y=train['Survived']\n",
    "features = [\"Pclass\", \"Sex\", \"SibSp\", \"Parch\",\"Title\",\"Cabin\",\"Fam_size\",\"Embarked\"]\n",
    "# datas=datas.drop('Name',axis=1)\n",
    "# datas=datas.drop('Age',axis=1)\n",
    "# datas=datas.drop('Ticket',axis=1)\n",
    "# datas=datas.drop('Fam_type',axis=1)\n",
    "# datas=datas.drop('Fare',axis=1)\n",
    "# qq=pd.get_dummies(datas)\n",
    "train=datas[datas['Survived'].notnull()]\n",
    "test=datas[datas['Survived'].isnull()].drop('Survived',axis=1)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e2c38f8e-ffe1-4961-b121-fbe7e66ae636",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
